﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
using System.Text.RegularExpressions;
using PaliTextReader2.Utilities;

namespace PaliTextReader2
{
    public partial class DictionaryControl : UserControl
    {
        public DictionaryControl()
        {
            InitializeComponent();
        }

        private void QuickDictionaryControl_Load(object sender, EventArgs e)
        {
            //ctlDictionaries.DataSource = m_objDictionariesTA.GetData();
            ctlSearchMode.SelectedIndex = 0;
            //ctlDictionaries.SelectedIndex = 0;
        }

        private void ctlFind_Click(object sender, EventArgs e)
        {
            DoSearch(ctlSearchBox.Text);
        }

        public void DoSearch(string strText)
        {
            ctlSearchBox.Text = strText;
            ctlTranslation.Text = String.Empty;
            if (strText != String.Empty)
            {
                Cursor.Current = Cursors.WaitCursor;
                try
                {
                    ContentsDS.DictionaryContentDataTable objPEDDTable = null;
                    if (ctlIgnoreDiac.Checked)
                        objPEDDTable = m_objContentsTA.FindNoDiac(ctlSearchMode.SelectedIndex,                            
                            2,
                            Utilities.Utility.ConvertDiac(ctlSearchBox.Text.Trim()));
                    else
                        objPEDDTable = m_objContentsTA.FindPali(ctlSearchMode.SelectedIndex,
                            2,
                            ctlSearchBox.Text.Trim());

                    ctlFoundItems.DataSource = objPEDDTable;
                    ctlResultsTab.SelectedIndex = 0;

                    if (objPEDDTable.Rows.Count > 0)
                        ctlSearchResults.Text = String.Empty;
                    else
                        ctlSearchResults.Text = "no results";
                }
                finally
                {
                    Cursor.Current = Cursors.Default;
                }
            }
        }

        //ctlTranslation.Text = Regex.Replace(objPEDDTable[0].Translation, "(" + Regex.Escape(ctlSearchBox.Text) + ")", "<$1>", RegexOptions.IgnoreCase);
       //ctlTranslation.Rtf = ctlTranslation.Rtf.Replace("<", @"{\b ").Replace(">","}");
        private void ctlFoundItems_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ctlFoundItems.SelectedValue != null)
            {
                ContentsDS.DictionaryContentDataTable objPEDDTable = m_objContentsTA.GetTranslation((int)ctlFoundItems.SelectedValue);
                FormatTranslation(objPEDDTable[0].Translation);
                ctlHistory.Items.Insert(0, ctlFoundItems.SelectedItem);
            }
        }

        private void ctlSearchBox_KeyDown(object sender, KeyEventArgs e)
        {
            PaliKeyboard.HandlePaliKeys(ctlSearchBox, e);

            if(e.KeyCode==Keys.Enter)
                DoSearch(ctlSearchBox.Text);
        }

        private void ctlHistory_SelectedIndexChanged(object sender, EventArgs e)
        {
            ContentsDS.DictionaryContentRow objRow=(ContentsDS.DictionaryContentRow)((DataRowView)ctlHistory.SelectedItem).Row;
            ContentsDS.DictionaryContentDataTable objPEDDTable = m_objContentsTA.GetTranslation(objRow.EntryID);
            FormatTranslation(objPEDDTable[0].Translation);
        }

        private void FormatTranslation(string strTranslation)
        {
            string strFormatted = strTranslation.Replace("[c]","");
            strFormatted = strFormatted.Replace("[/c]","\r\n");
            ctlTranslation.Text = strFormatted;
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ctlFoundItems.SelectedValue != null)
            {
                AddTermDlg dlg = new AddTermDlg((int)ctlFoundItems.SelectedValue);
                if (dlg.ShowDialog() == DialogResult.OK)
                {
                    ContentsDS.DictionaryContentRow objRow = (ContentsDS.DictionaryContentRow)((DataRowView)ctlFoundItems.SelectedItem).Row;
                    ContentsDS.DictionaryContentDataTable objPEDDTable = m_objContentsTA.GetTranslation(objRow.EntryID);
                    FormatTranslation(objPEDDTable[0].Translation);
                    objRow.Keyword = objPEDDTable[0].Keyword;
                }
            }
        }

        private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (ctlFoundItems.SelectedValue != null)
            {
                if (MessageBox.Show("Are you sure?", "Confirm", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    m_objContentsTA.Delete((int)ctlFoundItems.SelectedValue);
                    DoSearch(ctlSearchBox.Text);
                }
            }
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AddTermDlg dlg = new AddTermDlg();
            dlg.ShowDialog();
        }
    }
}
